<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Auth;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\ScenicArea;
use support\Request;
use support\Response;
use plugin\admin\app\model\ScenicSpot;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 景点列表 
 */
class ScenicSpotController extends Crud
{
    
    /**
     * @var ScenicSpot
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new ScenicSpot;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        $scenicArea = ScenicArea::where('id', 1)->first();
        $spots = ScenicSpot::where('area_id', 1)->get()->toArray();
        $markers = [];

        foreach ($spots as $spot) {
            $markers[] = ['position' => ['lat' => $spot['latitude'], 'lng' => $spot['longitude']], 'title' => $spot['name'], 'id' => $spot['id']];
        }

        return view('scenic-spot/index', [
            'area' => $scenicArea,
            'markers' => $markers
        ]);
    }

    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        $password_filed = 'password';
        if (isset($data[$password_filed])) {
            $data[$password_filed] = Util::passwordHash($data[$password_filed]);
        }

        if (!Auth::isSupperAdmin() && $this->dataLimit) {
            if (!empty($data[$this->dataLimitField])) {
                $admin_id = $data[$this->dataLimitField];
                if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                    throw new BusinessException('无数据权限');
                }
            }
        }

        $data['created_time'] = date('Y-m-d H:i:s');

        ScenicArea::where('id', $data['area_id'])->increment('scenic_spot_count');

//        if (empty($data['video'])) {
//            throw new \Exception('请上传视频');
//        }

        return $data;
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }

        return view('scenic-spot/insert', [
            'lng' => $request->get('lng'),
            'lat' => $request->get('lat'),
        ]);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        $scenicSpot = ScenicSpot::where('id', $request->get('id'))->first();
        $images = explode(',', $scenicSpot['images']);

        return view('scenic-spot/update', [
            'images' => $images
        ]);
    }

    protected function doDelete(array $ids)
    {
        if (!$ids) {
            return;
        }
        $primary_key = $this->model->getKeyName();
        $res = $this->model->whereIn($primary_key, $ids)->get()->toArray();

        foreach ($res as $value) {
            ScenicArea::where('id', $value['area_id'])->decrement('scenic_spot_count');
        }

        $this->model->whereIn($primary_key, $ids)->delete();
    }
}
